/*
package com.graduate.controller;

import com.db.pojo.Picture;
import com.graduate.exception.BizException;
import com.graduate.exception.ExceptionEnum;
import com.graduate.utils.BaseServiceUtils;
import com.graduate.utils.FileUtils;
import com.graduate.service.ImageService;
import com.graduate.utils.RespBody;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;


@RestController
@EnableAutoConfiguration
@RequestMapping("/image")
public class ImageController1 extends BaseServiceUtils {

*/
/**
     * 读取文件上传后的存储根路径
     *//*




    @Value("${web-file-upload-path}")
    private String fileUploadPath;

    @Autowired
    ImageService imageService;


    @PostMapping(value = "/upload")
    @ResponseBody
    public RespBody uploadImage(
            @RequestBody MultipartFile file
    ) {
        RespBody result = RespBody.success();

        String newFileName = FileUtils.saveImageToLocal(file, fileUploadPath);
        if (StringUtils.isEmpty(newFileName)) {
            throw new BizException(ExceptionEnum.IMAGE_UPLOAD_ERROR);
        } else {
            result.setRespMsg("上传成功");
            result.setResult(newFileName);
        }
        return result;
    }

    @SneakyThrows
    @PostMapping(value = "/insert")
    @ResponseBody
    @ApiOperation(value = "上传图片(新)")
    @LogAnnotation(title = "图片管理", action = "上传图片接口(新)")
    public RespBody insertImage(
            @RequestBody MultipartFile file
    ) {
        RespBody result = RespBody.success();
        String id = SnowFlake.getInstance().nextStringId("im");
        String oldFileName = file.getOriginalFilename();
        String newFileName = FileUtils.generateNewFileName(oldFileName);
        Picture picture = new Picture();
        picture.setId(id);
        picture.setName(newFileName);
        picture.setType(ConstantConfig.TYPE_IMAGE_BASE64);
        File tempFile = File.createTempFile(FileUtils.getPrefix(newFileName), FileUtils.getSuffixWithDot(newFileName));
        file.transferTo(tempFile);
        String originalBase64 = FileUtils.file2Base64(tempFile);
        String suffixWithoutDot = FileUtils.getSuffixWithoutDot(newFileName);
        String imageBase64 = "data:image/" + suffixWithoutDot + ";base64," + originalBase64;
        picture.setContent(imageBase64);
        int res = imageService.insertImage(imageReqVO);
        if (res == 0) {
            throw new BizException(ExceptionEnum.IMAGE_UPLOAD_ERROR);
        } else {
            result.setRespMsg("上传成功");
            result.setResult(newFileName);
        }
        return result;
    }


*/
/*
     * 这个方法最主要的就是要 @GetMapping(value = "/get", produces = MediaType.IMAGE_JPEG_VALUE)
     * 写 produces 否则无法获取到的是 text 格式
     *//*




    @ApiOperation(value = "获取图片(通过图片名/id/belongId)")
    @GetMapping(value = "/get", produces = MediaType.IMAGE_JPEG_VALUE)
    @LogAnnotation(title = "图片管理", action = "获取图片(通过图片名/id/belongId)接口")
    public byte[] getImageByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "belongId", required = false) String belongId
    ) throws IOException {
        if (StringUtils.isEmpty(name)
                && StringUtils.isEmpty(id)
                && StringUtils.isEmpty(belongId)) {
            throw new BizException(ExceptionEnum.PARAM_IS_NULL);
        }
        Set<String> set = new LinkedHashSet<>();
        set.add(name);
        set.add(id);
        set.add(belongId);
        set.removeAll(Collections.singleton(null));
        if (set.size() != 1) {
            throw new BizException(ExceptionEnum.BODY_NOT_MATCH);
        }
        Picture picture = null;
        if (!StringUtils.isEmpty(name)) {
            if (name.length() > 255) {
                throw new BizException(ExceptionEnum.BODY_NOT_MATCH);
            }
            picture = imageService.selectImageByName(name);
        }
        if (!StringUtils.isEmpty(id)) {
            if (id.length() > 32) {
                throw new BizException(ExceptionEnum.BODY_NOT_MATCH);
            }
            picture = imageService.selectImageById(id);
        }
        if (!StringUtils.isEmpty(belongId)) {
            if (belongId.length() > 32) {
                throw new BizException(ExceptionEnum.BODY_NOT_MATCH);
            }
            picture = imageService.selectImageByBelongId(belongId);
        }
        if (picture == null) {
            throw new BizException(ExceptionEnum.DATA_IS_NULL);
        }
        String imageBase64 = picture.getContent();
        String originalBase64 = imageBase64.substring(imageBase64.indexOf(";base64,") + 8);
        File tempFile = File.createTempFile(FileUtils.getPrefix(name), FileUtils.getSuffixWithDot(name));
        tempFile = FileUtils.base64ToFile(originalBase64, tempFile);
        FileInputStream inputStream = new FileInputStream(tempFile);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }


*/
/*
     * 注意这里是下载，而不是读取
     * 读取直接走静态资源配置 RESTFul 风格请求
     * downloadFile() 方法内部进行 setContentType("application/octet-stream")
     * 因此不会有返回值，直接数据流响应
     *//*



    @ApiOperation(value = "下载图片")
    @GetMapping("/download")
    @LogAnnotation(title = "图片管理", action = "下载图片接口")
    public void downloadImage(@RequestParam(value = "imageName") String imageName,
                              HttpServletResponse resp) throws IOException {
        // 注意区分 ClassPathResource 与 FileSystemResource
        Resource resource = new FileSystemResource(fileUploadPath + imageName);
        FileUtils.downloadFile(resource, resp);
    }


*/
/*
     *读取图片
     *//*



    @ApiOpedration(value = "读取图片")
    @GetMapping(value = "/{imageName}", produces = MediaType.IMAGE_JPEG_VALUE)
    @LogAnnotation(title = "图片管理", action = "读取图片接口")

    public byte[] getImage(HttpServletRequest request,
                           HttpServletResponse response,
                           @PathVariable(value = "imageName") String imageName) throws IOException {
        File file = new File(fileUploadPath + imageName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }
}
*/
